package com.wu.server.controller;

import com.wu.server.code.Code;
import com.wu.server.entity.User;
import com.wu.server.service.UserServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
public class UserController {

    @Autowired
    Code code;
    @Autowired
    UserServer userServer;
    @GetMapping("user")
    @ResponseBody
    public Object getUserInfo(@RequestParam(value = "userName") String userName, @RequestParam(value = "password") String password,@RequestParam(value = "isManager") Boolean isManager){
        return userServer.checkUser(userName,password,isManager);
    }
    @GetMapping("userList")
    @ResponseBody
    public Object getUserList(
            @RequestParam(value = "id",defaultValue = "") String id,
            @RequestParam(value = "nickName") String nickName,
            @RequestParam(value = "sort",defaultValue = "id") String sort,
            @RequestParam(value = "order",defaultValue = "desc") String order,
            @RequestParam(value = "pageNum") int pageNum,
            @RequestParam(value = "pageSize") int pageSize)
    {
        return userServer.getUserList(id,nickName,sort,order,pageNum,pageSize);
    }
    @PostMapping("user")
    @ResponseBody
    public Object postUserInfo(@RequestBody User form){
        return userServer.saveUserInfo(form);
    }
    @PutMapping("user")
    @ResponseBody
    public Object updateUserInfo(@RequestBody User form){
        return userServer.updateUserInfo(form);
    }
    @DeleteMapping("user")
    @ResponseBody
    public Object deleteUserInfo(@RequestParam(value = "id") String id){
        if (id == null ||id.equals("")) return code.getMeta(Code.PARAM_ERROR,null);
        return userServer.deleteUserInfo(id);
    }
}
